package com.dmcb.trade.business.crawlers.author;

import com.dmcb.common.business.constants.CommonConstant;
import com.dmcb.common.business.utils.JsonUtil;
import com.dmcb.common.business.utils.RedisUtils;
import com.dmcb.common.business.utils.WebUtil;
import com.dmcb.common.web.conversion.JsonResult;
import com.dmcb.trade.business.constants.CrawlConstant;
import com.dmcb.trade.business.crawlers.article.KuaiBaoParser;
import com.dmcb.trade.business.entities.Author;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by 黑米 on 2017/2/5.
 */
@Service
public class KuaiBaoCrawler extends BaseCrawler {
    Logger logger = LoggerFactory.getLogger(KuaiBaoCrawler.class);

    @Autowired
    private KuaiBaoParser kuaiBaoParser;

    @Override
    protected String url() {
        return CrawlConstant.KUAIBAO_URL;
    }

    @Override
    protected int tag() {
        return CrawlConstant.KUAIBAO_TAG;
    }

    @Override
    protected String avatarSelect() {
        return "div.author-pic>img";
    }

    @Override
    protected String channelNameSelect() {
        return "div.author-name";
    }


    @Override
    protected String listSelect() {
        return "div.feeds>div";
    }

    @Override
    protected String titleSelect() {
        return "div.feeds-item>h3>a";
    }

    @Override
    protected String authorSelect() {
        return "div.author-name";
    }

    @Override
    protected String authorLink() {
        return "div.chlname>a";
    }

    @Override
    protected JsonResult crawl(int authorId, String link, Author author, Document doc, boolean all) {
        // 爬取文章 快报一次能获取所有数据列表
        //http://kuaibao.qq.com/s/MEDIANEWSLIST?chlid=5423015
        int totalAll = 0;
        List<String> list = new ArrayList<String>();
        String SId = link.substring(link.indexOf("=") + 1, link.length());
        link = "http://r.cnews.qq.com/getSubNewsIndex?chlid=" + SId;
        logger.info("快报导入文章链接=" + link);
        String json = WebUtil.buildRequest(link,true);
        Map<String, Object> maps = JsonUtil.getResult(json);
        List<Map<String, String>> mapList = (List) maps.get("ids");
        for (Map<String, String> map : mapList) {
            String id = map.get("id");
            if (StringUtils.isBlank(id)) {
                continue;
            }
            list.add("http://kuaibao.qq.com/s/" + id);
        }
        JsonResult result = kuaiBaoIterate(authorId, list);
        totalAll += result.getInt();
        return totalAll == 0 ? error(CrawlConstant.MSG_WARN_NO_UPDATE) : success(totalAll);
    }

    /**
     * 迭代解析、保存文章
     *
     * @return 成功保存数量
     */
    protected JsonResult kuaiBaoIterate(int authorId, List<String> list) {
        int succ = 0;
        int ero = 0;
        //连续失败十次，结束循环，成功一次，失败次数重新累计
        for (String link : list) {
            try {
                Thread.sleep(10000);
                JsonResult result = parse(authorId, link);
                int status = result.getStatus();
                if (status != JsonResult.STATUS_SUCCESS) {
                    if(status == JsonResult.STATUS_ERROR){
                        ero++;
                        if (ero >= 10) {
                            break;
                        }
                    }
                   if(status == JsonResult.STATUS_WARN){
                       RedisUtils.changeIP(15);
                   }
                    continue;
                }
                succ += result.isSuccess() ? 1 : 0;
                ero = 0;
            } catch (Exception e) {
                logger.error("Parser.parse error,{}", link, e);
                return error(CommonConstant.ABNORMAL_MSG);
            }
        }
        return success(succ);
    }


    @Override
    protected JsonResult iterate(int authorId, Document doc) {
        return super.iterate(authorId, doc);
    }

    @Override
    protected JsonResult parse(int authorId, String link) throws Exception {
        return kuaiBaoParser.parse(authorId, link);
    }

    @Override
    protected String getBody(Document doc) {
        return kuaiBaoParser.getBody(doc);
    }
}

